<!DOCTYPE html>

<html lang="en">
<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  <meta name="baidu-site-verification" content="GOgEP8c1pN"/>
  

  <title>
    基于类的通用视图：ListView 和 DetailView_Django博客教程_追梦人物的博客</title>

  <link href="font-awesome.min.css"
        rel="stylesheet">
  <link rel="stylesheet" href="mobi.min.css">
  <link rel="stylesheet" href="modal.min.css">
  <link rel="stylesheet" href="friendly.css">
  <link rel="stylesheet" href="blog.css?v0.12">
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="/static/blog/js/menu.js"></script>
  <script src="/static/blog/js/modal.min.js"></script>
  <script src="/static/blog/js/blog.js"></script>
  

  
    <script>
        // baidu statistics
        var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?fb59b2a6022bccc02671a750f61c356b";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();

        // baidu auto push
        (function () {
            var bp = document.createElement('script');
            var curProtocol = window.location.protocol.split(':')[0];
            if (curProtocol === 'https') {
                bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
            }
            else {
                bp.src = 'http://push.zhanzhang.baidu.com/push.js';
            }
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(bp, s);
        })();
    </script>
    
  
</head>
<body class="bg-light">
<header>
  <nav class="flex-center bg-white nav-bar">
    <div class="container-wider flex-middle">
      
  <a class="show-on-mobile nav-link m-5" id="js-sidebar-btn" href="#" role="button">
    <i class="fa fa-chevron-right" aria-hidden="true"></i>
  </a>

      <a class="nav-bar-logo mr-7" href="/">
        <img src="/static/blog/images/logo.png" alt="blog logo">
      </a>
      <ul class="unit-0 hide-on-mobile nav-bar-nav">
        <li class="nav-item mr-7">
          <a class="nav-link" href="/">首页</a>
        </li>
        <li class="nav-item mr-7">
          <a class="nav-link" href="/tutorials/">教程</a>
        </li>
        <li class="nav-item mr-7">
          <a class="nav-link" href="/categories/">分类</a>
        </li>
        <li class="nav-item mr-7">
          <a class="nav-link" href="/archives/">归档</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/donate/">赞助</a>
        </li>
      </ul>
      <div class="unit flex-right flex-middle">
        <form class="hide-on-mobile flex-left flex-middle search-form mr-5" id="search-form"
              action="/search/">
          <input class="unit" type="search" name="q" placeholder="搜索..."/>
          <button class="unit-0" type="submit" role="button">
            <i class="fa fa-search" aria-hidden="true"></i>
          </button>
        </form>
        <a class="show-on-mobile nav-link fs-7 mr-7" id="js-search-btn" href="#" role="button">
          <i class="fa fa-search" aria-hidden="true"></i>
        </a>
        <div class="flex-middle notification">
          
            <a class="nav-link" href="/accounts/login/">
              <i class="fa fa-sign-in" aria-hidden="true"></i>
            </a>
          
        </div>
        <div class="show-on-mobile flex-middle">
          <a class="nav-link fs-7 ml-7 mr-5" id="menu-toggle" href="#">
            <i class="fa fa-ellipsis-v" aria-hidden="true"></i>
          </a>
          <ul class="menu bg-white" data-menu data-menu-toggle="#menu-toggle">
            <li class="nav-item mr-7">
              <a class="nav-link" href="/">首页</a>
            </li>
            <li>
              <a href="/tutorials/">教程</a>
            </li>
            <li>
              <a href="/categories/">分类</a>
            </li>
            <li>
              <a href="/archives/">归档</a>
            </li>
            <li>
              <a href="/donate/">赞助</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </nav>
</header>
<section class="flex-center">
  <div class="container-wider">
    <!-- 模态框（Modal） -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
              &times;
            </button>
            <h4 class="modal-title" id="myModalLabel">设置评论回复接收邮箱</h4>
            <div class="text-primary mt-2">邮箱仅用于接收回复提醒，不会被公开。</div>
            
          </div>
          <div class="modal-body">
            <form class="form email-binding-form" action="/comments/ajax_email_binding/">
              <div class="message">
                <span class="text-danger"></span>
              </div>
              <input type='hidden' name='csrfmiddlewaretoken' value='bcuMadWRMRiHb3Llp74vh9V1q5YSmkCcapsMGNqwxoFCdnQvmRwNXJsxKRtY6a7m' />
              <div class="flex-left units-gap">
                <label class="unit-0 text-right" for="multiple-inputs-name">邮箱: </label>
                <div class="unit">
                  <input type="email" name="email" id="id_email" placeholder="请输入绑定邮箱"/>
                </div>
                <div class="unit-0">
                  <button class="btn" id="js-send" type="button"
                          data-target="/comments/ajax_verification_code/">发送验证码
                  </button>
                </div>
              </div>
              <div class="flex-left units-gap">
                <label class="unit-0 text-right" for="multiple-inputs-name">验证码: </label>
                <div class="unit">
                  <input type="text" name="verification_code" id="id_verification_code"
                         placeholder="请输入接收到的验证码"/>
                </div>
              </div>
              <div class="flex-right">
                <button type="button" class="btn btn-danger mr-3" data-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary" id="js-submit">设置</button>
              </div>
            </form>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal -->
    </div>
    <div class="flex-left flex-wrap "
         style="margin-top: 60px">
      
  <aside class="unit-1-4 unit-1-on-mobile top-gap">
    <div class="scroll-view bg-white card toc-sidebar" style="height: 100%">
      <div class="card-header">
        目录
      </div>
      <div class="flex-left">
        <div class="unit-1-4">
          <a href="https://promotion.aliyun.com/ntms/act/group/team.html?group=FcfAncJPAp" target="_blank"><img
              src="/static/blog/images/aliyun.jpg" alt=""></a>
        </div>
        <div class="unit-1-2 flex-vertical top-gap text-small">
          <a style="color: #e83e8c" target="_blank"
             href="https://promotion.aliyun.com/ntms/act/group/team.html?group=FcfAncJPAp">云服务器新用户</a>
          <a style="color: #6610f2" target="_blank"
             href="https://promotion.aliyun.com/ntms/act/group/team.html?group=FcfAncJPAp">8元/月</a>
        </div>
      </div>
      <div class="card-body">
        <ul>
          <li>
            <h6 class="top-gap-0"><a href="">Django博客教程</a></h6>
            <ul>
              
                <li>
                  <a href="2.html">前言</a>
                  
                </li>
              
                <li>
                  <a href="3.html">搭建开发环境</a>
                  
                </li>
              
                <li>
                  <a href="4.html">建立 Django 博客应用</a>
                  
                </li>
              
                <li>
                  <a href="5.html">创建 Django 博客的数据库模型</a>
                  
                </li>
              
                <li>
                  <a href="6.html">让 Django 完成翻译：迁移数据库</a>
                  
                </li>
              
                <li>
                  <a href="7.html">Django 博客首页视图</a>
                  
                </li>
              
                <li>
                  <a href="8.html">真正的 Django 博客首页视图</a>
                  
                </li>
              
                <li>
                  <a href="9.html">在 Django Admin 后台发布文章</a>
                  
                </li>
              
                <li>
                  <a href="10.html">博客文章详情页</a>
                  
                </li>
              
                <li>
                  <a href="11.html">支持 Markdown 语法和代码高亮</a>
                  
                </li>
              
                <li>
                  <a href="12.html">页面侧边栏：使用自定义模板标签</a>
                  
                </li>
              
                <li>
                  <a href="13.html">分类与归档</a>
                  
                </li>
              
                <li>
                  <a href="14.html">评论</a>
                  
                </li>
              
                <li>
                  <a href="16.html">已知小问题修正</a>
                  
                </li>
              
                <li>
                  <a href="20.html">使用 Nginx 和 Gunicorn 部署 Django 博客</a>
                  
                </li>
              
                <li>
                  <a href="21.html">使用 Fabric 自动化部署</a>
                  
                </li>
              
                <li>
                  <a href="29.html">统计文章阅读量</a>
                  
                </li>
              
                <li>
                  <a href="32.html">自动生成文章摘要</a>
                  
                </li>
              
                <li>
                  <a href="33.html">基于类的通用视图：ListView 和 DetailView</a>
                  
                    <div class="toc">
<ul>
<li><a href="#listview">ListView</a><ul>
<li><a href="#将-index-视图函数改写为类视图">将 index 视图函数改写为类视图</a></li>
<li><a href="#将-category-视图函数改写为类视图">将 category 视图函数改写为类视图</a></li>
<li><a href="#将-archives-视图函数改写成类视图">将 archives 视图函数改写成类视图</a></li>
</ul>
</li>
<li><a href="#detailview">DetailView</a></li>
<li><a href="#总结">总结</a></li>
</ul>
</div>

                  
                </li>
              
                <li>
                  <a href="34.html">Django Pagination 简单分页</a>
                  
                </li>
              
                <li>
                  <a href="37.html">Django Pagination 完善分页</a>
                  
                </li>
              
                <li>
                  <a href="38.html">统计各个分类下的文章数</a>
                  
                </li>
              
                <li>
                  <a href="39.html">标签云</a>
                  
                </li>
              
                <li>
                  <a href="41.html">RSS 订阅</a>
                  
                </li>
              
                <li>
                  <a href="43.html">Markdown 自动生成文章目录</a>
                  
                </li>
              
                <li>
                  <a href="44.html">简单全文搜索</a>
                  
                </li>
              
                <li>
                  <a href="45.html">Django Haystack 全文检索与关键词高亮</a>
                  
                </li>
              
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </aside>

      
  <div class="unit-3-4 unit-1-on-mobile top-gap">
    


<div class="bg-white card post-card px-7 py-5">
  
<article class="post">
  <h1 class="top-gap-0">基于类的通用视图：ListView 和 DetailView</h1>
  <ul class="list-inline dot-divider post-meta">
    <li class="list-inline-item text-small text-muted">
      10 月，2 周前
    </li>
    <li class="list-inline-item text-small text-muted">7177 字
    </li>
    <li class="list-inline-item text-small text-muted">
      13140 阅读
    </li>
    <li class="list-inline-item text-small text-muted">
      46 评论
    </li>
  </ul>
  <div class="top-gap-big post-body">
    <p>在开发网站的过程中，有一些视图函数虽然处理的对象不同，但是其大致的代码逻辑是一样的。比如一个博客和一个论坛，通常其首页都是展示一系列的文章列表或者帖子列表。对处理首页的视图函数来说，虽然其处理的对象一个是文章，另一个是帖子，但是其处理的过程是非常类似的。首先是从数据库取出文章或者帖子列表，然后将这些数据传递给模板并渲染模板。于是，Django 把这些相同的逻辑代码抽取了出来，写成了一系列的通用视图函数，即基于类的通用视图（Class Based View）。</p>
<p>使用类视图是 Django 推荐的做法，而且熟悉了类视图的使用方法后，能够减少视图函数的重复代码，节省开发时间。接下来就让我们把博客应用中的视图函数改成基于类的通用视图。</p>
<h2 id="listview">ListView</h2>
<p>在我们的博客应用中，有几个视图函数是从数据库中获取文章（Post）列表数据的：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">views</span><span class="o">.</span><span class="n">py</span>

<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># ...</span>

<span class="k">def</span> <span class="nf">archives</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
    <span class="c1"># ...</span>

<span class="k">def</span> <span class="nf">category</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">pk</span><span class="p">):</span>
    <span class="c1"># ...</span>
</pre></div>


<p>这些视图函数都是从数据库中获取文章（Post）列表，唯一的区别就是获取的文章列表可能不同。比如 <code>index</code> 获取全部文章列表，<code>category</code> 获取某个分类下的文章列表。</p>
<h3 id="将-index-视图函数改写为类视图">将 index 视图函数改写为类视图</h3>
<p>针对这种从数据库中获取某个模型列表数据（比如这里的 Post 列表）的视图，Django 专门提供了一个 <code>ListView</code> 类视图。下面我们通过一个例子来看看 <code>ListView</code>  的使用方法。我们首先把 <code>index</code> 视图函数改造成类视图函数。</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">views</span><span class="o">.</span><span class="n">py</span>

<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">ListView</span>

<span class="k">class</span> <span class="nc">IndexView</span><span class="p">(</span><span class="n">ListView</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Post</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;blog/index.html&#39;</span>
    <span class="n">context_object_name</span> <span class="o">=</span> <span class="s1">&#39;post_list&#39;</span>
</pre></div>


<p>要写一个类视图，首先需要继承 Django 提供的某个类视图。至于继承哪个类视图，需要根据你的视图功能而定。比如这里 <code>IndexView</code> 的功能是从数据库中获取文章（Post）列表，<code>ListView</code> 就是从数据库中获取某个模型列表数据的，所以 <code>IndexView</code> 继承 <code>ListView</code>。</p>
<p>然后就是通过一些属性来指定这个视图函数需要做的事情。这里我们指定了三个属性。</p>
<ul>
<li>model。将 model 指定为 <code>Post</code>，告诉 Django 我要获取的模型是 <code>Post</code>。</li>
<li>template_name。指定这个视图渲染的模板。</li>
<li>context_object_name。指定获取的模型列表数据保存的变量名。这个变量会被传递给模板。</li>
</ul>
<p>如果还是有点难以理解，不妨将类视图的代码和 <code>index</code> 视图函数的代码对比一下：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">views</span><span class="o">.</span><span class="n">py</span>

<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">post_list</span> <span class="o">=</span> <span class="n">Post</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;blog/index.html&#39;</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;post_list&#39;</span><span class="p">:</span> <span class="n">post_list</span><span class="p">})</span>
</pre></div>


<p><code>index</code> 视图函数首先通过 <code>Post.objects.all()</code> 从数据库中获取文章（Post）列表数据，并将其保存到 <code>post_list</code> 变量中。而在类视图中这个过程 <code>ListView</code>  已经帮我们做了。我们只需告诉 <code>ListView</code> 去数据库获取的模型是 <code>Post</code>，而不是 <code>Comment</code> 或者其它什么模型，即指定 <code>model = Post</code>。将获得的模型数据列表保存到 <code>post_list</code> 里，即指定 <code>context_object_name = 'post_list'</code>。然后渲染 blog/index.html 模板文件，<code>index</code> 视图函数中使用 <code>render</code> 函数。但这个过程 <code>ListView</code>   已经帮我们做了，我们只需指定渲染哪个模板即可。</p>
<p>接下来就是要将类视图转换成函数视图。为什么需要将类视图转换成函数视图呢？</p>
<p>我们来看一看 blog 的 URL 配置：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">urls</span><span class="o">.</span><span class="n">py</span>

<span class="n">app_name</span> <span class="o">=</span> <span class="s1">&#39;blog&#39;</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
    <span class="o">...</span>
<span class="p">]</span>
</pre></div>


<p>前面已经说过每一个 URL 对应着一个视图函数，这样当用户访问这个 URL 时，Django 就知道调用哪个视图函数去处理这个请求了。在 Django 中 URL 模式的配置方式就是通过 <code>url</code> 函数将 URL 和视图函数绑定。比如 <code>url(r'^$', views.index, name='index')</code>，它的第一个参数是 URL 模式，第二个参数是视图函数 <code>index</code>。对 <code>url</code> 函数来说，第二个参数传入的值必须是一个函数。而 <code>IndexView</code> 是一个类，不能直接替代 <code>index</code> 函数。好在将类视图转换成函数视图非常简单，只需调用类视图的 <code>as_view()</code> 方法即可（至于 <code>as_view</code> 方法究竟是如何将一个类转换成一个函数的目前不必关心，只需要在配置 URL 模式是调用 <code>as_view</code> 方法就可以了。具体的实现我们以后会专门开辟一个专栏分析类视图的源代码，到时候就能看出 Django 使用的魔法了）。</p>
<p>现在在 URL 配置中把 <code>index</code> 视图替换成类视图 <code>IndexView</code> ：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">urls</span><span class="o">.</span><span class="n">py</span>

<span class="n">app_name</span> <span class="o">=</span> <span class="s1">&#39;blog&#39;</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">IndexView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;index&#39;</span><span class="p">),</span>
    <span class="o">...</span>
<span class="p">]</span>
</pre></div>


<p>访问一下首页，可以看到首页依然显示全部文章列表，和使用视图函数 <code>index</code> 时效果一模一样。</p>
<h3 id="将-category-视图函数改写为类视图">将 category 视图函数改写为类视图</h3>
<p><code>category</code> 视图函数的功能也是从数据库中获取文章列表数据，不过其和 <code>index</code> 视图函数不同的是，它获取的是某个分类下的全部文章。因此 <code>category</code>  视图函数中多了一步，即首先需要根据从 URL 中捕获的分类 id 并从数据库获取分类，然后使用 <code>filter</code> 函数过滤出该分类下的全部文章。来看看这种情况下类视图该怎么写：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">views</span><span class="o">.</span><span class="n">py</span>

<span class="k">class</span> <span class="nc">CategoryView</span><span class="p">(</span><span class="n">ListView</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Post</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;blog/index.html&#39;</span>
    <span class="n">context_object_name</span> <span class="o">=</span> <span class="s1">&#39;post_list&#39;</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">cate</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Category</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;pk&#39;</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">CategoryView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">category</span><span class="o">=</span><span class="n">cate</span><span class="p">)</span>
</pre></div>


<p>和 <code>IndexView</code> 不同的地方是，我们覆写了父类的 <code>get_queryset</code> 方法。该方法默认获取指定模型的全部列表数据。为了获取指定分类下的文章列表数据，我们覆写该方法，改变它的默认行为。</p>
<p>首先是需要根据从 URL 中捕获的分类 id（也就是 pk）获取分类，这和 <code>category</code> 视图函数中的过程是一样的。不过注意一点的是，在类视图中，从 URL 捕获的命名组参数值保存在实例的 <code>kwargs</code> 属性（是一个字典）里，非命名组参数值保存在实例的 <code>args</code> 属性（是一个列表）里。所以我们使了 <code>self.kwargs.get('pk')</code> 来获取从 URL 捕获的分类 id 值。然后我们调用父类的 <code>get_queryset</code> 方法获得全部文章列表，紧接着就对返回的结果调用了 <code>filter</code> 方法来筛选该分类下的全部文章并返回。</p>
<p>此外我们可以看到 <code>CategoryView</code> 类中指定的属性值和 <code>IndexView</code>  中是一模一样的，所以如果为了进一步节省代码，甚至可以直接继承 <code>IndexView</code>：</p>
<div class="codehilite"><pre><span></span><span class="k">class</span> <span class="nc">CategoryView</span><span class="p">(</span><span class="n">IndexView</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">cate</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Category</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;pk&#39;</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">CategoryView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">category</span><span class="o">=</span><span class="n">cate</span><span class="p">)</span>
</pre></div>


<p>然后就在 URL 配置中把 <code>category</code> 视图替换成类视图 <code>CategoryView</code> ：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">urls</span><span class="o">.</span><span class="n">py</span>

<span class="n">app_name</span> <span class="o">=</span> <span class="s1">&#39;blog&#39;</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="o">...</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^category/(?P&lt;pk&gt;[0-9]+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">CategoryView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;category&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>


<p>访问以下某个分类页面，可以看到依然显示的是该分类下的全部文章列表，和使用视图函数<code>category</code> 时效果一模一样。</p>
<h3 id="将-archives-视图函数改写成类视图">将 archives 视图函数改写成类视图</h3>
<p>这里没有什么新东西要讲了，学以致用，这个任务就交给你自己了。如果还是不会写的请参考本章节的 GitHub 代码 <a href="https://github.com/zmrenwu/django-blog-tutorial/tree/Step18_class-based-views">Step18: class based views</a>。</p>
<h2 id="detailview">DetailView</h2>
<p>除了从数据库中获取模型列表的数据外，从数据库获取模型的一条记录数据也是常见的需求。比如查看某篇文章的详情，就是从数据库中获取这篇文章的记录然后渲染模板。对于这种类型的需求，Django 提供了一个 <code>DetailView</code> 类视图。下面我们就来将 <code>detail</code> 视图函数转换为等价的类视图 <code>PostDetailView</code>，代码如下：</p>
<div class="codehilite"><pre><span></span><span class="n">blog</span><span class="o">/</span><span class="n">views</span><span class="o">.</span><span class="n">py</span>

<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">ListView</span><span class="p">,</span> <span class="n">DetailView</span>

<span class="c1"># 记得在顶部导入 DetailView</span>
<span class="k">class</span> <span class="nc">PostDetailView</span><span class="p">(</span><span class="n">DetailView</span><span class="p">):</span>
    <span class="c1"># 这些属性的含义和 ListView 是一样的</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Post</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;blog/detail.html&#39;</span>
    <span class="n">context_object_name</span> <span class="o">=</span> <span class="s1">&#39;post&#39;</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># 覆写 get 方法的目的是因为每当文章被访问一次，就得将文章阅读量 +1</span>
        <span class="c1"># get 方法返回的是一个 HttpResponse 实例</span>
        <span class="c1"># 之所以需要先调用父类的 get 方法，是因为只有当 get 方法被调用后，</span>
        <span class="c1"># 才有 self.object 属性，其值为 Post 模型实例，即被访问的文章 post</span>
        <span class="n">response</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">PostDetailView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># 将文章阅读量 +1</span>
        <span class="c1"># 注意 self.object 的值就是被访问的文章 post</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">increase_views</span><span class="p">()</span>

        <span class="c1"># 视图必须返回一个 HttpResponse 对象</span>
        <span class="k">return</span> <span class="n">response</span>

    <span class="k">def</span> <span class="nf">get_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="c1"># 覆写 get_object 方法的目的是因为需要对 post 的 body 值进行渲染</span>
        <span class="n">post</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">PostDetailView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_object</span><span class="p">(</span><span class="n">queryset</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
        <span class="n">post</span><span class="o">.</span><span class="n">body</span> <span class="o">=</span> <span class="n">markdown</span><span class="o">.</span><span class="n">markdown</span><span class="p">(</span><span class="n">post</span><span class="o">.</span><span class="n">body</span><span class="p">,</span>
                                      <span class="n">extensions</span><span class="o">=</span><span class="p">[</span>
                                          <span class="s1">&#39;markdown.extensions.extra&#39;</span><span class="p">,</span>
                                          <span class="s1">&#39;markdown.extensions.codehilite&#39;</span><span class="p">,</span>
                                          <span class="s1">&#39;markdown.extensions.toc&#39;</span><span class="p">,</span>
                                      <span class="p">])</span>
        <span class="k">return</span> <span class="n">post</span>

    <span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># 覆写 get_context_data 的目的是因为除了将 post 传递给模板外（DetailView 已经帮我们完成），</span>
        <span class="c1"># 还要把评论表单、post 下的评论列表传递给模板。</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">PostDetailView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">CommentForm</span><span class="p">()</span>
        <span class="n">comment_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">comment_set</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
            <span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
            <span class="s1">&#39;comment_list&#39;</span><span class="p">:</span> <span class="n">comment_list</span>
        <span class="p">})</span>
        <span class="k">return</span> <span class="n">context</span>
</pre></div>


<p><code>PostDetailView</code> 稍微复杂一点，主要是等价的 <code>detail</code> 视图函数本来就比较复杂，下面来一步步对照 <code>detail</code> 视图函数中的代码讲解。</p>
<p>首先我们为 <code>PostDetailView</code> 类指定了一些属性的值，这些属性的含义和 <code>ListView</code> 中是一样的，这里不再重复讲解。</p>
<p>紧接着我们覆写了 <code>get</code> 方法。这对应着 <code>detail</code> 视图函数中将 post 的阅读量 +1 的那部分代码。事实上，你可以简单地把 <code>get</code> 方法的调用看成是 <code>detail</code> 视图函数的调用。</p>
<p>接着我们又复写了 <code>get_object</code> 方法。这对应着 <code>detail</code> 视图函数中根据文章的 id（也就是 pk）获取文章，然后对文章的 post.body 进行 Markdown 渲染的代码部分。</p>
<p>最后我们复写了 <code>get_context_data</code> 方法。这部分对应着 <code>detail</code> 视图函数中生成评论表单、获取 post 下的评论列表的代码部分。这个方法返回的值是一个字典，这个字典就是模板变量字典，最终会被传递给模板。</p>
<p>你也许会被这么多方法搞乱，为了便于理解，你可以简单地把 <code>get</code> 方法看成是 <code>detail</code> 视图函数，至于其它的像 <code>get_object</code>、<code>get_context_data</code> 都是辅助方法，这些方法最终在 <code>get</code> 方法中被调用，这里你没有看到被调用的原因是它们隐含在了 <code>super(PostDetailView, self).get(request, *args, **kwargs)</code> 即父类 <code>get</code> 方法的调用中。最终传递给浏览器的 HTTP 响应就是 <code>get</code> 方法返回的 <code>HttpResponse</code> 对象。</p>
<p>还是无法理解么？在不涉及源码的情况下我也只能讲这么多了。要想熟练掌握并灵活运用类视图必须仔细阅读类视图的源码，我当时也是啃源码啃了很久很久，以后我会专门开辟一个专题分析类视图的源码，到时候你就会对类视图有更深的理解了。此外，这里是 Django 官方文档对类视图的讲解，尽管我觉得这部分文档对类视图也讲得不是很清楚，不过也值得作为参考吧 <a href="https://docs.djangoproject.com/en/1.10/topics/class-based-views/">基于类的视图概述</a>。</p>
<h2 id="总结">总结</h2>
<p>本章节的代码位于：<a href="https://github.com/zmrenwu/django-blog-tutorial/tree/Step18_class-based-views">Step18: class based views</a>。</p>
<p>如果遇到问题，请通过下面的方式寻求帮助。</p>
<ul>
<li>在下方评论区留言。</li>
<li>将问题的详细描述通过邮件发送到 djangostudyteam@163.com，一般会在 24 小时内回复。</li>
<li>在 <a href="http://www.pythonzh.cn/category/newbie-qa/">Pythonzhcn 社区的新手问答版块</a> 发布帖子。</li>
</ul>
    <p class="text-center">
      -- EOF --
    </p>
    <div>
      <button class="btn btn-primary" id="js-reward"><i class="fa fa-jpy"
                                                        aria-hidden="true"></i>
        如果觉得文章对你有帮助，请随意打赏
        <i class="fa fa-chevron-down" aria-hidden="true"></i>
      </button>
      <div class="flex-left flex-wrap units-gap-big mt-4" style="display: none">
        <div class="unit unit-1-on-mobile flex-center">
          <figure>
            <img src="/static/blog/images/weixinpay.jpg" alt="微信支付收款二维码"
                 style="width: 250px">
            <figcaption class="text-center fs-6 mt-2" style="color: green">微信</figcaption>
          </figure>
        </div>
        <div class="unit unit-1-on-mobile flex-center">
          <figure>
            <img src="/static/blog/images/alipay.jpg" alt="支付宝收款二维码" style="width: 250px">
            <figcaption class="text-center fs-6 mt-2 text-primary">支付宝</figcaption>
          </figure>
        </div>
      </div>
    </div>
    <div class="post-tags">
      <ul class="list-inline">
        
      </ul>
    </div>
  </div>
</article>
  <div class="top-gap">
    <div class="jiathis_style">
      <span class="jiathis_txt">分享到：</span>
      <a class="jiathis_button_qzone">QQ空间</a>
      <a class="jiathis_button_tsina">新浪微博</a>
      <a class="jiathis_button_tqq">腾讯微博</a>
      <a class="jiathis_button_weixin">微信</a>
      <a href="http://www.jiathis.com/share?uid=2128865"
         class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis"
         target="_blank">更多</a>
      <a class="jiathis_counter_style"></a>
    </div>
    <script type="text/javascript">
        var jiathis_config = {data_track_clickback: 'true'};
    </script>
    <script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=2128865"
            charset="utf-8"></script>
    <!-- JiaThis Button END -->
  </div>
  <br>
  <div class="top-gap clearfix">
    
      <a href="32.html">
        <i class="fa fa-angle-double-left" aria-hidden="true"></i>
        自动生成文章摘要
      </a>
    
    
      <a class="float-right" href="34.html">
        Django Pagination 简单分页
        <i class="fa fa-angle-double-right" aria-hidden="true"></i>
      </a>
    
  </div>
  

  <section class="comment-area top-gap-big" id="comment-area">
    <h5>46 条评论 / 20 人参与</h5>
    
      
<div class="flex-center top-gap login-panel p-7">
  <div>
    <div class="text-muted text-center login-header"><span>登录后回复</span></div>
    <div class="flex-center text-center social-icons mt-3">
                        <span class="weibo mr-3">
                            <a href="/accounts/weibo/login/">
                                <i class="fa fa-weibo" aria-hidden="true"></i>
                            </a>
                        </span>
      <span class="github">
                            <a href="/accounts/github/login/">
                                <i class="fa fa-github" aria-hidden="true"></i>
                            </a>
                        </span>
    </div>
  </div>
</div>
    
    <div class="top-gap-big comment-list">
  
    <div class="flex-left py-4" id="c2212">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars1.githubusercontent.com/u/20534739?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">silencehero
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>博主改为detailview报这个错，No module named 'mdx_markdown'&nbsp;</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">2 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/2212"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
        <div class="comment-descendant-list">
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c2128">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars1.githubusercontent.com/u/31177490?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">Zeng1998
          
        </span>
    
  </header>
  <div class="comment-body">
    <pre><code><br>class CategoryView(ListView):<br />    model = Post<br />    template_name = 'blog/index.html'<br />    context_object_name = 'post_list'<br /><br /><br />    def get_queryset(self):<br />        cate = get_object_or_404(Category, pk=self.kwargs.get('pk'))<br />        return super(CategoryView, self).get_queryset().filter(category=cate)<br></code></pre><p>博主，这段代码能不能直接改成<br></p><pre><code>class CategoryView(ListView):<br />    model = Post<br />    template_name = 'blog/index.html'<br />    context_object_name = 'post_list'<br /><br /><br />    def get_queryset(self):<br />        return super(CategoryView, self).get_queryset().filter(pk=self.kwargs.get('pk')）<br></code></pre><p>感觉效果一样啊 为什么要多那一步找出对应的分类</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">3 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/2128"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c2141">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">Zeng1998
        

  </header>
  <div class="comment-body">
    <p>看上去效果并不等价哦，实现的功能是筛选该分类下的全部文章。修改后并没有筛选分类下的文章。</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">2 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/2141"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1975">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tva3.sinaimg.cn/crop.0.0.180.180.180/572e7280jw1e8qgp5bmzyj2050050aa8.jpg"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">哈哈餐馆
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>关于改写&nbsp;ListView 有个问题想请教大家和博主大人。</p><p>我使用的版本是 Django 1.11.7 然后之前看过1.11 的官方入门教程，如果按照官方的改写方法，以&nbsp;CategoryView 为例吧，大概会写成这样：</p><pre><code class="lang-python">class CategoryView(generic.ListView):<br />    model = Post<br />    template_name = 'blog/index.html'<br />    context_object_name = 'post_list'<br /><br />    def get_queryset(self):<br />        cate = get_object_or_404(Category, pk=self.kwargs.get('pk'))<br />        return Post.objects.filter(category=cate)<br></code></pre><p>最后一句的返回写法和博主教程的不一样</p><pre><code>return super(CategoryView, self).get_queryset().filter(category=cate)<br></code></pre><p>这两种方法在前端看起来页面都是一样的。</p><p><br></p><p>我想请问：这两种方法在后端或者说 Python 层面来看是一样的么？（比如说效率什么的）那种更好？在我小白看来似乎官方写法更省字</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">3 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1975"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
        <div class="comment-descendant-list">
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1795">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars0.githubusercontent.com/u/15711752?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">point6013
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>博主，我有一个问题想请教，在通用视图中，DetailView中我想向模板中添加其他的内容，而且该内容是根据post内容通过调用其他方法生成的，添加时间，我已经完成，在get_context_data中可以实现，但是其他的内容如何添加，也就是说在get_context_data中如何调用request和pk。或者有没有其他方案？烦请指导。</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">4 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1795"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c1801">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">point6013
        

  </header>
  <div class="comment-body">
    <p>你可以使用 self.object 获取到和这个 view 关联的 post 对象，例如self.object.pk。self.request 获取到关联的 http 请求对象。</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">4 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1801"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c1818">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars0.githubusercontent.com/u/15711752?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">point6013
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    <p>回去测试一下，非常感谢您的教程，慢慢开始入门django了</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">4 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1818"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1773">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/32827300?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">fshgrym
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>多写几遍就理解了哈</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">4 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1773"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
        <div class="comment-descendant-list">
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1682">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars0.githubusercontent.com/u/24841991?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">igmpxb
          
        </span>
    
  </header>
  <div class="comment-body">
    <p style="">有一个问题呢</p><p style="">在没有用试图类之前我们是这样获取文章列表的，这个是按时间·排序了的<br></p><p style="">post_list = Post.objects.all().order_by('-creater_time')</p><p style=""><br></p><p style="">可是</p><p style="">&nbsp;class IndexView(ListView): <br></p><p style="">model = Post <br></p><p style="">template_name = 'blog/index.html' <br></p><p style="">    context_object_name = 'post_list'</p><p style="">这样以后就没有按时间排序的效果了= =。&nbsp; <br></p><p style=""><br></p><p style="">大家都是怎么弄的呢<br></p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1682"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c1683">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars0.githubusercontent.com/u/24841991?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">igmpxb
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">igmpxb
        

  </header>
  <div class="comment-body">
    <p style="">晕 这个问题我在往后面看的时候解决了，解决的方法是重写get_queryset该方法，目测是可行的，大家看看这样解有问题吗？</p><p style="">def get_queryset(self):</p><p style="margin-left: 40px;">    return super(IndexView, self).get_queryset().order_by('-creater_time')</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1683"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c1772">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tva3.sinaimg.cn/crop.53.213.533.533.180/005Hctu9jw8fbx217tvp0j30hs0qot90.jpg"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">大圣______
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">igmpxb
        

  </header>
  <div class="comment-body">
    <p style="">前面的教程里，博主写在Post模块里了<br></p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">4 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1772"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1666">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/26378937?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">luoliang
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>大佬，为什么我按着你代码敲的，改成视图函数后PostDetailView。每次点一个文章，阅读数怎么没保存上？</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1666"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c1668">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">luoliang
        

  </header>
  <div class="comment-body">
    <p>show me the code.</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1668"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1653">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tva3.sinaimg.cn/crop.0.0.996.996.180/c4f50241jw8fb552bmg0zj20ro0rotag.jpg"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">荧惑1516
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>博主你好，我换成了mysql数据库，但是这样导致了明明有文章，归档也有显示时间，但是点击时间后显示没有文化在那个提示，换回sqlite就好了，而且只有归档有bug，其他没有，请问怎么解决啊</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，2 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1653"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c1674">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">荧惑1516
        

  </header>
  <div class="comment-body">
    <p>好像是 mysql 的时间存储的缘故，评论区里找一找，好像有人提过解决方案</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1674"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c1676">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tva3.sinaimg.cn/crop.0.0.996.996.180/c4f50241jw8fb552bmg0zj20ro0rotag.jpg"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">荧惑1516
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    <p>好的，谢谢博主<br><br></p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">5 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1676"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1469">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/17308070?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">hesiyu
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>博主你好，我CategoryView继承了IndexView，但是我想让index和category都支持markdown，怎么把get_queryset整合成一个，或者直接从IndexView继承过来呢？</p><pre><code class="lang-python">class CategoryView(IndexView):<br />    def get_queryset(self):<br />        cate = get_object_or_404(Category, pk=self.kwargs.get('pk'))<br />        return super(CategoryView, self).get_queryset().filter(category=cate)<br /><br />    def get_queryset(self):<br />        post_list = Post.objects.all().order_by('-created_time')<br />        for post in post_list:<br />            post.excerpt = markdown.markdown(post.excerpt,<br />                                             extensions=[<br />                                                 'markdown.extensions.extra',<br />                                                 'markdown.extensions.codehilite',<br />                                                 'markdown.extensions.toc',<br />                                             ])<br />        return post_list<br></code></pre>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">6 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1469"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c1470">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">hesiyu
        

  </header>
  <div class="comment-body">
    <p>可以继承呀！</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">6 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1470"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1454">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tvax3.sinaimg.cn/crop.0.0.580.580.180/006nPFwsly8fjh60cy1czj30g40g4gns.jpg"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">王_小_花_
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>博主你好，我有一个疑问：</p><p>DetailView中用来获取指定post的那行代码post = super(PostDetailView, self).get_object(queryset=None)，并没有传入关于pk的参数，是怎么按照文章的pk查找的？另外，queryset的作用是什么？</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">6 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1454"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
        <div class="comment-descendant-list">
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1405">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/31658250?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">mamami
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>找到错误了，IndexView我把它定义成函数了</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">6 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1405"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
        <div class="comment-descendant-list">
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c1404">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/31658250?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">mamami
          
        </span>
    
  </header>
  <div class="comment-body">
    <p>from django.views.generic import ListView，这个我的导入了，但是显示没有使用，显示错误：<br></p><p>AttributeError: 'function' object has no attribute 'as_view' ，<br></p><p>找到ListView源代码，显示只有class ListView(MultipleObjectTemplateResponseMixin, BaseListView):</p><p>里头啥也没有</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">6 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1404"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
        <div class="comment-descendant-list">
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c976">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tvax1.sinaimg.cn/default/images/default_avatar_male_180.gif"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">dackzome
          
        </span>
    
  </header>
  <div class="comment-body">
    大佬，使用完新的通用视图类，我发现首页的评论数量消失了，不能统计数量。没改新的视图之前，是能正常显示的。是否因为在class IndexView中，只传了post_list的缘故？应该怎么做才可以让他继续显示正常？
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/976"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c985">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">dackzome
        

  </header>
  <div class="comment-body">
    这看你代码怎么写的，评论数量可以使用类似于 post.comment_set.count() 的代码获取的。你可以通过视图函数传给模板，也可以直接在模板中调用。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/985"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c987">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tvax1.sinaimg.cn/default/images/default_avatar_male_180.gif"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">dackzome
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    <span class="comments-link"><a href="#">{{comment_list.count}} 评论</a></span>这是之前的，在detail.html中可以正常显示，但是在index.html中不能正常显示。现在在index.html中用了   <span class="comments-link"><a href="#">{{post.comment_set.count}} 评论</a></span>，index.html能正常显示了，感谢博主。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/987"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c878">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tvax3.sinaimg.cn/default/images/default_avatar_male_180.gif"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">科兴一把刀
          
        </span>
    
  </header>
  <div class="comment-body">
    博主，PostDetailView那里我试了一下，不复写get函数，直接把阅读量+1这个函数放到get_object里面也可以：<br />    def get_object(self, queryset=None):<br />        post = super(PostDetailView, self).get_object(queryset=None)<br />        # 阅读量+1<br />        post.increase_views()<br />        # 其他代码...<br />        return post<br />能否说说get和get_object的区别呢？
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/878"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c886">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">科兴一把刀
        

  </header>
  <div class="comment-body">
    可能是因为 get 方法中调用了 get_object 方法，推荐放在 get 中，这样更加直观。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/886"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c958">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tvax3.sinaimg.cn/default/images/default_avatar_male_180.gif"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">科兴一把刀
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    嗯，这样思路是比较清晰
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/958"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c830">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tva1.sinaimg.cn/crop.0.0.996.996.180/c5f3b6b6jw8f3cpekexmaj20ro0rpdh7.jpg"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">知而不倦
          
        </span>
    
  </header>
  <div class="comment-body">
    感谢博主，你讲得很好的，至少是我目前看到过最好的教材，内容也丰富，而且通俗易懂。刚接触django感觉好难，直接看官方中文翻译文章看的云里雾里，根本不知道讲什么，而且文档太长，我觉得有些的功能也很少用到的，也不知道重点，文档虽然很全，但是感觉讲的好乱。后来看的一些入门教材，能看懂，感觉讲的内容太少的，也没有结合实际项目实践。后面看了一些视频，对django的主要功能有些了解，视频不好笔记，也不好复习，过段时间又忘了，主要是自己没有练吧！跟着你的教程一步步来，还有源码的。有些感觉的。真的感谢。可以的话也可以写本书的，flask有本书感觉还不错，感觉市场上django的好书太少，之前买了一本挺厚的，翻翻就不想看了，干货太少废话连篇，讲的也很基础，还不如网上的入门教材。就不说是哪本书了。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月，2 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/830"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c900">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">知而不倦
        

  </header>
  <div class="comment-body">
    谢谢你的回复，希望教程对大家有帮助。django 开发的 90% 的东西都在文档中，文档是基础。但是如你所说，文档全面但是零散。所以我就写一些实战项目教程，作为文档的补充。也帮助大家在实战中学习开发 django。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/900"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c817">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tvax4.sinaimg.cn/default/images/default_avatar_male_180.gif"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">严健菊_144
          
        </span>
    
  </header>
  <div class="comment-body">
    class base():<br />    def __init__(self):<br />        #下面的index和show方法都传值data，我想在__init__这边就要传递，有什么方法不？<br />        pass<br />        <br />    def index(self,request):<br />        data = 1<br />        return render(request,'index.html',{'data':data})<br />        <br />    def show(self,request):<br />        data = 1<br />        return render(request,'show.html',{'data':data})
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月，2 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/817"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c905">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">严健菊_144
        

  </header>
  <div class="comment-body">
    没有明白你的意思，你的意思是这样么？def __init__(self, data=1)
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">8 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/905"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c571">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/19380238?v=4"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">sakura1357
          
        </span>
    
  </header>
  <div class="comment-body">
    博主报错： ImproperlyConfigured at 4.html<br />DetailView is missing a QuerySet. Define DetailView.model, DetailView.queryset, or override DetailView.get_queryset().<br />Request Method:	GET<br />Request URL:	http://127.0.0.1:80004.html<br />Django Version:	1.10.6<br />Exception Type:	ImproperlyConfigured<br />Exception Value:	<br />DetailView is missing a QuerySet. Define DetailView.model, DetailView.queryset, or override DetailView.get_queryset().<br /><br />提示没有QuerySet，我看你的代码POSTDetailView里面没有复写get_queryset()方法
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/571"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c572">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/19380238?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">sakura1357
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">sakura1357
        

  </header>
  <div class="comment-body">
    我在PostDetailView里面复写了这个方法之后还是报错： <br />‘’‘python<br />    def get_queryset(self):<br />        return super(PostDetailView, self).get_queryset()<br />'''
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/572"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c573">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">sakura1357
        

  </header>
  <div class="comment-body">
    类属性设置的和教程中的一致么？
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/573"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c577">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/19380238?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">sakura1357
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    一样的，都是这3个属性，我看了你在github上的本章节的源码，一模一样的代码。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/577"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c578">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">sakura1357
        

  </header>
  <div class="comment-body">
    如果代码和项目中的一样，那就不知道是哪里的问题了。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，1 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/578"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c1409">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars3.githubusercontent.com/u/31658250?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">mamami
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">sakura1357
        

  </header>
  <div class="comment-body">
    <p>blog/urls里你视图函数应该写错了，应该是views.PostDetailView.as_view()，我跟你错的一样的错，就是因为我把视图函数写成了views.DetailView.as_view()</p>
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">6 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/1409"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c399">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tvax4.sinaimg.cn/crop.0.0.996.996.180/aad1e30fly8feuipebw12j20ro0ro41f.jpg"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">JustBreaking
          
        </span>
    
  </header>
  <div class="comment-body">
    评论不支持删除功能呀，博主可以加上<br />另外我想问的是 detailsView类中， urls 中的文章pk参数是怎么传进去的？
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/399"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c401">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">JustBreaking
        

  </header>
  <div class="comment-body">
    django 帮我们自动把从 url 捕获的参数保存在了 self.kwargs 属性里。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/401"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c402">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tvax4.sinaimg.cn/crop.0.0.996.996.180/aad1e30fly8feuipebw12j20ro0ro41f.jpg"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">JustBreaking
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">JustBreaking
        

  </header>
  <div class="comment-body">
    好吧 ，跟着博主写了一遍 ，豁然开朗，多谢多谢！！
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/402"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c403">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tvax4.sinaimg.cn/crop.0.0.996.996.180/aad1e30fly8feuipebw12j20ro0ro41f.jpg"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">JustBreaking
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    嗯  教程里面叙述了，我开始还没开完教程就开始问。习惯不好O(∩_∩)O~
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/403"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c398">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="http://tvax4.sinaimg.cn/crop.0.0.996.996.180/aad1e30fly8feuipebw12j20ro0ro41f.jpg"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">JustBreaking
          
        </span>
    
  </header>
  <div class="comment-body">
    如果要传给模板的数据不止post_list，这种方法还能用么？
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/398"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c400">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">JustBreaking
        

  </header>
  <div class="comment-body">
    你可以在 get_context 方法中插入任何你想传递的模板变量呀。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，4 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/400"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
              <hr class="m-0">
            
          
            <div class="flex-left py-4" id="c433">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="http://tvax4.sinaimg.cn/crop.0.0.996.996.180/aad1e30fly8feuipebw12j20ro0ro41f.jpg"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">JustBreaking
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">追梦人物
        
            <span class="master">[博主]</span>
        

  </header>
  <div class="comment-body">
    嗯 明白了 多谢
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">9 月，3 周前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/433"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
      <hr class="m-0">
    
  
    <div class="flex-left py-4" id="c342">
      <div class="unit-0">
        <img class="comment-avatar mr-3" src="https://avatars1.githubusercontent.com/u/6928895?v=3"/>
      </div>
      <div class="unit">
        <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">李伟
          
        </span>
    
  </header>
  <div class="comment-body">
    说实话类视图到DetailView的时候有点晕了。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">10 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/342"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>
        
          <hr class="">
        
        <div class="comment-descendant-list">
          
            <div class="flex-left py-4" id="c343">
              <div class="unit-0">
                <img class="comment-avatar mr-3" src="https://avatars2.githubusercontent.com/u/13174219?v=4"/>
              </div>
              <div class="unit">
                
  <div class="comment-main">
  <header class="comment-user">
        <span class="text-small text-muted nickname">追梦人物
          
            <span class="master">[博主]</span>
          
        </span>
    
    <span class="reply-label text-small text-muted">
                                                        <i class="fa fa-share" aria-hidden="true"></i>
                                                    </span>
    <span class="nickname text-small text-muted">李伟
        

  </header>
  <div class="comment-body">
    慢慢消化吧，确实不好理解。实在无法理解就还是用函数写法。
  </div>
  <footer class="comment-footer flex-left">
    <ul class="unit list-inline dot-divider">
      <li class="list-inline-item">
        <time class="text-small text-muted">10 月前</time>
      </li>
      <li class="list-inline-item">
        <a href="/comments/reply/343"
           class="reply-btn text-small text-muted">回复</a>
      </li>
    </ul>
  </footer>
</div>

              </div>
            </div>
            
          
        </div>
      </div>
    </div>
    
  
</div>
  </section>
</div>
  </div>

    </div>
  </div>
</section>
<footer class="bg-white py-5 mt-5 text-small text-muted">
  <div class="flex-center">
    <div>&copy; 2017 追梦人物的博客</div>
  </div>
  
  
  
</footer>
<script>
    $('[data-menu]').menu();

    var InterValObj; //timer变量，控制时间
    var curCount = 120;//当前剩余秒数

    $('#js-send').on('click', function (event) {
        var $this = $(this);
        var $targetURL = $this.attr('data-target');
        $.post(
            $targetURL,
            {email: $('#id_email').val()},
            function (data) {
                if (data.ok) {
                    $this.attr("disabled", "true");
                    $this.text("重新发送验证码" + "(" + curCount + ")");
                    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
                    $('.message')
                        .find('span')
                        .text(data.msg)
                } else {
                    $('.message').find('span').text(data.msg)
                }
            }
        );
        return false;
    });

    //timer处理函数
    function SetRemainTime() {
        if (curCount === 0) {
            window.clearInterval(InterValObj);//停止计时器
            $('#js-send').removeAttr("disabled");//启用按钮
            $('#js-send').text("重新发送验证码");
        }
        else {
            curCount--;
            $('#js-send').text("重新发送验证码" + "(" + curCount + ")");
        }
    }

    $('#js-submit').on('click', function (event) {
        var $this = $(this);
        var $form = $('.email-binding-form');
        var $targetURL = $form.attr('action');
        console.log($targetURL);

        $.post(
            $targetURL,
            {
                email: $('#id_email').val(),
                verification_code: $('#id_verification_code').val()
            },
            function (data) {
                if (data.ok) {
                    location.reload();
                } else {
                    $('.message')
                        .find('span')
                        .text(data.msg)
                }
            }
        );
        return false;
    });


    var editor = new Simditor({
        textarea: $('#id_comment'),
        placeholder: "提出你的见解...",
        toolbarFloat: false,
        cleanPaste: true,
        toolbar: [
            'bold',
            'italic',
            'underline',
            'strikethrough',
            'color',
            'ol',
            'ul',
            'blockquote',
            'code',
            'table',
            'link',
            'image',
            'hr'
        ]
    });
</script>


</body>
</html>